package cc.langk.web.remote;

import cc.langk.core.entity.Result;
import cc.langk.core.util.MD5;
import cc.langk.web.entity.SysStaff;
import cc.langk.web.handler.TokenHandler;
import cc.langk.web.services.SysStaffService;
import cc.langk.web.util.PasswdEncryptoUtil;
import cc.langk.web.util.TokenBuild;
import org.codehaus.groovy.syntax.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/staff")
public class SysStaffController {


    @Autowired
    private SysStaffService sysStaffService;
    @Autowired
    private TokenHandler tokenHandler;

    /**
     * 添加管理用户
     *
     * @return
     */
    @RequestMapping("/add")
    public Result addUser(String account, String passwd, String name, String code) {
        try {
            String cryptoPasswd = PasswdEncryptoUtil.passwdEncrypto(passwd);
            SysStaff sysStaff = new SysStaff();
            sysStaff.setUserAccount(account);
            sysStaff.setUserName(name);
            sysStaff.setUserStatus(SysStaff.NORMAL);
            sysStaff.setCreateTime(new Date());
            sysStaff.setStaffAccountCode(code);
            sysStaff.setUserPassword(cryptoPasswd);
            boolean dbResult = sysStaffService.savaSysStaff(sysStaff);
            if (dbResult) {
                return new Result.ResultBuiler<>().success();
            } else {
                return new Result.ResultBuiler<>().failed("添加失败");
            }
        }catch (Exception e){
            return new Result.ResultBuiler<>().error();
        }
    }


    /**
     * 删除管理用户
     *
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteUser(String account,String token) {
        try {
            boolean dbResult =  sysStaffService.deleteByUserAccount(account);
            if (dbResult) {
                return new Result.ResultBuiler<>().success();
            } else {
                return new Result.ResultBuiler<>().failed("删除失败");
            }
        }catch (Exception e){
            return new Result.ResultBuiler<>().error();
        }
    }

    /**
     * 更新管理用户
     *
     * @return
     */
    @RequestMapping("/upate")
    public Result upateUser(SysStaff sysStaff,String token) {
        try {
            boolean dbResult =  sysStaffService.updateById(sysStaff);
            if (dbResult) {
                return new Result.ResultBuiler<>().success();
            } else {
                return new Result.ResultBuiler<>().failed("更新失败");
            }
        }catch (Exception e){
            return new Result.ResultBuiler<>().error();
        }
    }


    /**
     * 查询管理用户
     *
     * @return
     */
    @RequestMapping("/select")
    public Result selectUser(String token) {
        try {
            List<SysStaff> list =  sysStaffService.selectAllUser();
            if (list!=null) {
                return new Result.ResultBuiler<>().success(list);
            } else {
                return new Result.ResultBuiler<>().failed("查询用户失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result.ResultBuiler<>().error();
        }
    }

    /**
     * 管理员登录
     * @param name
     * @param passwd
     * @return
     */
    @RequestMapping("/login")
    public Result login(@RequestParam String name,@RequestParam String passwd){
        try {
            SysStaff sysStaff = sysStaffService.login(name,passwd);
            if(sysStaff==null){
                return new Result.ResultBuiler<>().failed("用户名或密码错误");
            }
            String token = TokenBuild.buildToken();
            tokenHandler.putToken(token,sysStaff.getRowId()+"");
            return new Result.ResultBuiler<>().success(token);
        }catch (Exception e){
            e.printStackTrace();
            return new Result.ResultBuiler<>().error();
        }
    }

}
